package com.aoxon.cargo.component;

import java.util.List;

import android.app.Activity;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.loader.NormalDataLoader;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.ToastUtil;
import com.aoxon.cargo.view.MyGridView;
import com.aoxon.cargo.view.MyScrollView;

/**
 * 供应端物品瀑布流的数据加载显示
 * 
 * @author ZZJ
 * 
 */
public class WaterFallShowGoods implements MyScrollView.OnScrollListener,
		NormalDataLoader.OnLoadDataListener {

	private Activity activity;

	private TextView tvLoading;
	private ProgressBar progressBar;
	private RelativeLayout rlListBottom;

	private MyGridView myGridView;
	private MyScrollView myScrollView;

	private BaseAdapter adapter;

	public int length = 12;// 每次加载的个数
	public int startIndex = 0; // 显示的总个数

	private List<?> list;

	private boolean isReachLast = false;
	private NormalDataLoader normalDataLoader;

	public WaterFallShowGoods(Activity activity, BaseAdapter adapter,
			List<?> list) {

		this.activity = activity;

		tvLoading = (TextView) activity.findViewById(R.id.tvListBottom);
		progressBar = (ProgressBar) activity.findViewById(R.id.pbListBottom);
		rlListBottom = (RelativeLayout) activity
				.findViewById(R.id.rlListBottom);

		myScrollView = (MyScrollView) activity
				.findViewById(R.id.msvPurGridShowGoods);
		myGridView = (MyGridView) activity
				.findViewById(R.id.mgvPurGridShowGoods);

		normalDataLoader = new NormalDataLoader(activity);
		normalDataLoader.setOnloadDataListener(this);

		this.adapter = adapter;
		this.list = list;

		myScrollView.getView();
		myScrollView.setOnScrollListener(this);

		myGridView.setAdapter(adapter);
		adapter.notifyDataSetChanged();

	}

	public void setListFlag(int flag) {
		myScrollView.setVisibility(flag);
	}

	/**
	 * 设置进度条状态
	 */
	public void setProgressFlag(int flag) {
		rlListBottom.setVisibility(flag);
	}

	/**
	 * 设置显示的列数
	 * 
	 * @param num
	 */
	public void setNumColumns(int num) {
		myGridView.setNumColumns(num);
	}

	/**
	 * 获取当前的GridView
	 * 
	 * @return
	 */
	public GridView getGridView() {
		return myGridView;
	}

	/**
	 * 清空所有列中的子项显示和列表数据
	 */
	public void clearView() {
		startIndex = 0;
		isReachLast = false;
		list.clear();
		adapter.notifyDataSetChanged();

	}

	/**
	 * 设置当前列表数据
	 * 
	 * @param list
	 */
	public void setList(List<?> list) {
		this.list = list;
	}

	@Override
	public void onBottom() {
		// TODO Auto-generated method stub
		if (!isReachLast) {
			normalDataLoader.load();
		}
	}

	@Override
	public void onTop() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScroll() {
		// TODO Auto-generated method stub

	}

	// 即将到达底部（2/3处）
	@Override
	public void onWillToBottom() {
		if (!isReachLast) {
			normalDataLoader.load();
		}
	}

	public void loadData() {
		normalDataLoader.load();
	}

	public interface ToLoad {
		public boolean doLoad() throws Exception;
	}

	public interface Command {
		public void doCommand();
	}

	private ToLoad toLoad;

	public void setToLoad(ToLoad toLoad) {
		this.toLoad = toLoad;
	}

	@Override
	public void beforeLoad() {
		// TODO Auto-generated method stub
		tvLoading.setText("加载中...");
		progressBar.setVisibility(View.VISIBLE);
	}

	@Override
	public void toLoad(Message msg) throws Exception {
		// TODO Auto-generated method stub
		if (toLoad.doLoad()) {
			msg.what = MyStateUtil.LIST_CHANGE;
		} else {
			// 查找不到数据的情况
			isReachLast = true;
			msg.what = MyStateUtil.LIST_IS_REACH_LAST;
		}
	}

	@Override
	public void afterLoad(int state) {
		// TODO Auto-generated method stub
		progressBar.setVisibility(View.GONE);
		switch (state) {
		case MyStateUtil.LIST_CHANGE:
			Log.i("listChange",
					"------------------listChange--------------------");
			tvLoading.setText("向上拉加载数据");
			adapter.notifyDataSetChanged();
			break;
		case MyStateUtil.LIST_IS_REACH_LAST:
			tvLoading.setText("没有相应数据咯～");
			break;
		case MyStateUtil.NOT_NET_CONNECTION:
			tvLoading.setText("无网络连接～");
			break;
		case MyStateUtil.ERROR_NET_CONNECTION:
			tvLoading.setText("向上拉加载数据");
			ToastUtil.show(activity, "网络连接异常");
			break;
		default:
			break;
		}
	}

}
